package com.youi.third.yibaopay;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.Properties;

import com.yeepay.g3.sdk.yop.client.YopRequest;
import com.yeepay.g3.sdk.yop.client.YopResponse;
import com.yeepay.g3.sdk.yop.client.YopRsaClient;
import com.youi.third.wechat.util.WechatUtil;
import com.youi.third.yibaopay.model.HmacKey;
import com.youi.util.JsonUtil;

/**
 * 签名工具
 * @author 86135
 *
 */
public class HmacUtil {

	public static String PRIVATEKEY=null;//平台商密钥
	public static String ParentMerchantNo=null;//系统商编	
	public static String MerchantNo=null;//子商户编号
	public static String NotifyUrl=null;//支付成功服务器回调地址
	public static String RefundNotifyUrl=null;//退款成功服务器回调地址


	/**
	 * 加载配置文件
	 */
	static{
        Properties prop = new Properties();
        InputStream in = WechatUtil.class.getResourceAsStream("/yibaopay.properties");
		try {
		    prop.load(in);
		    String prfix="yibaopay.";//前缀
		    PRIVATEKEY=prop.getProperty(prfix+"PRIVATEKEY").trim();
		    ParentMerchantNo=prop.getProperty(prfix+"ParentMerchantNo").trim();
		    MerchantNo=prop.getProperty(prfix+"MerchantNo").trim();
		    NotifyUrl=prop.getProperty(prfix+"NotifyUrl");
		    RefundNotifyUrl=prop.getProperty(prfix+"RefundNotifyUrl".trim());
	    } catch (IOException e) {
	        e.printStackTrace();
	    }
	}
	
	public HmacUtil(String PRIVATEKEY,String ParentMerchantNo,String MerchantNo,String NotifyUrl) {
		this.PRIVATEKEY=PRIVATEKEY;
		this.ParentMerchantNo=ParentMerchantNo;
		this.MerchantNo=MerchantNo;
		this.NotifyUrl=NotifyUrl;
	}
			
			/**
			 * 生成创建订单hmac方法
			 * @param paramsHmac
			 * @return
			 * @throws IOException
			 */
			public static  String gethamc(Map<String, String> paramsHmac) throws IOException{
				StringBuffer hmacBuffer = new StringBuffer();
				//请调商户密钥获取接口获取子商户密钥
				String hmackey=getHmackey().getMerHmacKey();
				String[] paramHmac={"parentMerchantNo","merchantNo","orderId","orderAmount","notifyUrl"};
				for(int i = 0; i < paramHmac.length; i++){
					String key = paramHmac[i];
					hmacBuffer.append(key).append("=").append(paramsHmac.get(key)).append("&");
				}
				String hmacStr = hmacBuffer.subSequence(0, hmacBuffer.length()-1).toString();
				System.out.println("hmacStr:"+hmacStr);
				System.out.println("hmackey:"+hmackey);
				String hmac = Md5Utils.encoderHmacSha256(hmacStr, hmackey);
			    
				return hmac;
			}
			
			public static  String getYibaoOrderhamc(Map<String, String> paramsHmac) throws IOException{
				StringBuffer hmacBuffer = new StringBuffer();
				//请调商户密钥获取接口获取子商户密钥
				String hmackey=getHmackey().getMerHmacKey();
				String[] paramHmac={"parentMerchantNo","merchantNo","orderId","uniqueOrderNo"};
				for(int i = 0; i < paramHmac.length; i++){
					String key = paramHmac[i];
					hmacBuffer.append(key).append("=").append(paramsHmac.get(key)).append("&");
				}
				String hmacStr = hmacBuffer.subSequence(0, hmacBuffer.length()-1).toString();
				System.out.println("hmacStr:"+hmacStr);
				System.out.println("hmackey:"+hmackey);
				String hmac = Md5Utils.encoderHmacSha256(hmacStr, hmackey);
			    
				return hmac;
			}
			
			/**
			 * 生成分账hmac方法
			 * @param paramsHmac
			 * @return
			 * @throws IOException
			 */
			public static String getdevidehamc(Map<String, String> paramsHmac) throws IOException{
				StringBuffer hmacBuffer = new StringBuffer();
				//请调商户密钥获取接口获取子商户密钥
				String hmackey=getHmackey().getMerHmacKey();
				String[] paramHmac={"parentMerchantNo","merchantNo","orderId","uniqueOrderNo","divideRequestId"};
				for(int i = 0; i < paramHmac.length; i++){
					String key = paramHmac[i];
					hmacBuffer.append(key).append("=").append(paramsHmac.get(key)).append("&");
				}
				String hmacStr = hmacBuffer.subSequence(0, hmacBuffer.length()-1).toString();
				System.out.println("hmacStr:"+hmacStr);
				System.out.println("hmackey:"+hmackey);
				String hmac = Md5Utils.encoderHmacSha256(hmacStr, hmackey);		    
				return hmac;
			}
			
			
			public static HmacKey getHmackey() throws IOException {
				YopRequest request = new YopRequest("OPR:"+ParentMerchantNo,PRIVATEKEY);
				request.addParam("parentMerchantNo", ParentMerchantNo);
				request.addParam("merchantNo", MerchantNo);
				YopResponse response = YopRsaClient.post("/rest/v1.0/sys/merchant/hmackeyquery", request);
				HmacKey hmacKey=null;
				if("FAILURE".equals(response.getState())){
					hmacKey=new HmacKey();
					hmacKey.setReturnCode(response.getError().getCode());
					hmacKey.setReturnMsg(response.getError().getMessage());
				}
				if (response.getResult()!=null) {
					hmacKey=JsonUtil.fromJson(response.getResult().toString(), HmacKey.class) ;
				}
				return hmacKey;			
			}
			
			/**
			 * 生成分账退款paramsHmac
			 * @return
			 * @throws IOException
			 */
			public static String getrefunddevidehamc(Map<String, String> paramsHmac) throws IOException{
				StringBuffer hmacBuffer = new StringBuffer();
				//请调商户密钥获取接口获取子商户密钥
				String hmackey=getHmackey().getMerHmacKey();
				String[] paramHmac={"parentMerchantNo","merchantNo","orderId","uniqueOrderNo","refundRequestId","refundAmount"};
				for(int i = 0; i < paramHmac.length; i++){
					String key = paramHmac[i];
					hmacBuffer.append(key).append("=").append(paramsHmac.get(key)).append("&");
				}
				String hmacStr = hmacBuffer.subSequence(0, hmacBuffer.length()-1).toString();
				System.out.println("hmacStr:"+hmacStr);
				System.out.println("hmackey:"+hmackey);
				String hmac = Md5Utils.encoderHmacSha256(hmacStr, hmackey);		    
				return hmac;
			}
			
			/**
			 * 生成资金全额确认签名
			 * @return
			 * @throws IOException
			 */
			public static String getfullsettlehamc(Map<String, String> paramsHmac) throws IOException{
				StringBuffer hmacBuffer = new StringBuffer();
				//请调商户密钥获取接口获取子商户密钥
				String hmackey=getHmackey().getMerHmacKey();
				String[] paramHmac={"parentMerchantNo","merchantNo","orderId","uniqueOrderNo"};
				for(int i = 0; i < paramHmac.length; i++){
					String key = paramHmac[i];
					hmacBuffer.append(key).append("=").append(paramsHmac.get(key)).append("&");
				}
				String hmacStr = hmacBuffer.subSequence(0, hmacBuffer.length()-1).toString();
				System.out.println("hmacStr:"+hmacStr);
				System.out.println("hmackey:"+hmackey);
				String hmac = Md5Utils.encoderHmacSha256(hmacStr, hmackey);		    
				return hmac;
			}
			
			/**
			 * 生成查询退款结果签名
			 * @return
			 * @throws IOException
			 */
			public static String getrefundhamc(Map<String, String> paramsHmac) throws IOException{
				StringBuffer hmacBuffer = new StringBuffer();
				//请调商户密钥获取接口获取子商户密钥
				String hmackey=getHmackey().getMerHmacKey();
				String[] paramHmac={"parentMerchantNo","merchantNo","refundRequestId","orderId","uniqueRefundNo"};
				for(int i = 0; i < paramHmac.length; i++){
					String key = paramHmac[i];
					hmacBuffer.append(key).append("=").append(paramsHmac.get(key)).append("&");
				}
				String hmacStr = hmacBuffer.subSequence(0, hmacBuffer.length()-1).toString();
				System.out.println("hmacStr:"+hmacStr);
				System.out.println("hmackey:"+hmackey);
				String hmac = Md5Utils.encoderHmacSha256(hmacStr, hmackey);		    
				return hmac;
			}
}
